<?php
class Administrator_UserSystemController extends AdministratorController implements IController{
	/**
	 * Model.
	 * @var Administrator_UserSystem_Model
	 */
	protected $_model;
	

	
/* (non-PHPdoc)
	 * @see AdministratorController::beforeSave()
	 */
	public function beforeSave($obj) {
		$user = $this->_model->getByID($obj);
		if(!isset($obj[UserSystem::PASSWORD]) || $obj[UserSystem::PASSWORD] == ""){
			if(isset($user))
				$obj[UserSystem::PASSWORD] = $user[UserSystem::PASSWORD];
		}
		else{
			$obj[UserSystem::PASSWORD] = StringUltil::encryptPassWord($obj[UserSystem::PASSWORD]);
		}
		$account = StringUltil::formatNormalString($obj[UserSystem::ACCOUNT]);
		$obj[UserSystem::ACCOUNT] = strtolower($account);
		if(isset($_FILES['UserSystem_image']) && $_FILES['UserSystem_image']["size"]>0){
			$imageName = $obj[UserSystem::ACCOUNT] + $obj[UserSystem::ID];
			$obj[UserSystem::IMAGE] = $this->uploadImage(UserSystem::IMAGE, $imageName);
		}
		else{
			if(isset($user))
				$obj[UserSystem::IMAGE] = $user[UserSystem::IMAGE];
		}
		return $obj;
	}

	// 	public function indexAction(){
// 		$this->view()->tableData = $this->_model->getAllJoin();
// 	}
	
	public function loginAction(){
		$this->view()->setLayoutName("Login");
		if(Auth::hasSession()){
			$this->_redirect('',true);
		}
		$returnURL = $this->getContext()->getRequest("return","");
		$username = "";
		$password = "";
		if($this->getContext()->isPost()){
			$username = $this->getContext()->postRequest("username","");
			$password = $this->getContext()->postRequest("password","");
			$result = $this->_model->login($username, $password);
			if($result == null){
				$this->view()->setHasError(true);
				$this->view()->setError($this->_model->getError());
			}
			else {
				Auth::startSession($result);
				$this->enableKCFinder();
				if($returnURL != "")
					$this->_redirect($returnURL,true);
				else
					$this->_redirect('/',true);
			}
		}
		$this->view()->username = $username;
		$this->view()->password = $password;
	}
	public function logoutAction(){
		$this->view()->setNoLayout();
		$this->view()->setNoView();
		$returnURL = $this->getContext()->getRequest("return","");
		Auth::endSession();
		$this->disableKCFinder();
		if($returnURL != "")
			$this->_redirect($returnURL,true);	
		else
			$this->_redirect('/',true);	
	}
	/* (non-PHPdoc)
	 * @see AdministratorController::initData()
	 */
	public function initData() {
		$this->view()->viewBag['RoleSources'] = $this->_model->getRoleForComboBox();
	}
	/* (non-PHPdoc)
	 * @see AdministratorController::validateData()
	 */
	public function validateData($data) {
		$result = array();
		$errors = array();
		if($data['account'] == null ||$data['account'] == ""){
			array_push($errors, "Bạn phải nhập account");
		}
		if($data['name'] == null ||$data['name'] == ""){
			array_push($errors, "Bạn phải nhập tên");
		}
		if($data['email'] == null ||$data['email'] == ""){
			array_push($errors, "Bạn phải nhập email");
		}
		if(count($errors)>0){
			$result['isError'] = true;
			$result['errors'] = $errors;
		}
		else {
			$result['isError'] = false;
			$result['errors'] = null;
		}
		$result['entity'] = $data;
		return $result;
	}

	private function disableKCFinder(){
		$_SESSION['KCFINDER'] = array();
		$_SESSION['KCFINDER']['disabled'] = true;
	}
	private function enableKCFinder(){
		$_SESSION['KCFINDER'] = array();
		$_SESSION['KCFINDER']['disabled'] = false;
	}


}